package 面试经典150题.滑动窗口;

/**
 * @author tmh
 * @date 2024/4/15 15:38
 * @description https://leetcode.cn/problems/minimum-size-subarray-sum/solutions/306066/javade-jie-fa-ji-bai-liao-9985de-yong-hu-by-sdwwld/?envType=study-plan-v2&envId=top-interview-150
 */
public class T209长度最小的子数组 {
    public int minSubArrayLen(int target, int[] nums) {
        int low = 0;
        int high = 0;
        int sum = 0;
        int min = Integer.MAX_VALUE;
        while (high < nums.length) {
            sum = sum + nums[high];
            high++;
            //当sum大于等于目标值的时候就从对头移除元素
            while (sum >= target) {
                //记录大于target的连续数组的长度
                min = Math.min(min, high - low);
                //这一步相当于是把队头元素个移除出去
                sum -= nums[low++];
            }
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }
}
